package com.springboot.template.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.springboot.template.domain.SecurityKeyInfo;
import com.springboot.template.domain.request.SecurityKeyInfoAddReq;
import com.springboot.template.domain.request.SecurityKeyInfoFetchReq;
import com.springboot.template.service.ISecurityKeyInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Zhang Yu
 * @description: TODO
 * @date 2024/5/28 10:59
 */
@RestController
@RequestMapping("/security")
@CrossOrigin
public class SecurityKeyInfoController {

    @Autowired
    ISecurityKeyInfoService securityKeyInfoService;

    /**
     * 新增密钥信息
     * @param securityKeyInfo
     * @return
     */
    @PostMapping("")
    public Object addSecurityKeyInfo(@RequestBody SecurityKeyInfo securityKeyInfo) {
        int i = securityKeyInfoService.insertSecurityKeyInfo(securityKeyInfo);
        JSONObject jsonObject = new JSONObject();
        if (i > 0) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "添加成功");
        }
        return jsonObject;
    }

    /**
     * 根据参数获取对应的密钥信息
     * @param securityKeyInfo
     * @return
     */
    @GetMapping("")
    public Object securityKeyInfoByInfo(SecurityKeyInfo securityKeyInfo) {
        SecurityKeyInfo securityKeyInfo1 = securityKeyInfoService.selectSecurityKeyInfoByInfo(securityKeyInfo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "请求成功");
        if (securityKeyInfo1 == null) {
            jsonObject.put("data",new JSONObject());
        }else {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("key", securityKeyInfo1.getContent());
            jsonObject.put("data",jsonObject1);
        }
        return jsonObject;
    }


    /**
     * 上传客户端公钥信息
     * @param securityKeyInfoAddReq content: 公钥信息 , channelId: 渠道标识(唯一主键), expireTime:过期时间(yyyy-MM-dd HH:mm:ss)
     * @return
     */
    @PostMapping("/rsa/client/public/key")
    public Object uploadClientRSAPublicKey(@RequestBody SecurityKeyInfoAddReq securityKeyInfoAddReq) {
        JSONObject jsonObject = new JSONObject();
        int result = securityKeyInfoService.uploadClientRSAPublicKey(securityKeyInfoAddReq);
        if (result > 0) {//成功
            jsonObject.put("code", 200);
            jsonObject.put("msg", "请求成功");
        }else {
            jsonObject.put("code", 500);
            jsonObject.put("msg", "请求失败");
        }
        return jsonObject;
    }


    /**
     * 获取服务端公钥和AES密钥
     * @param securityKeyInfoFetchReq
     * @return
     */
    @PostMapping("/server/key")
    public Object selectSecurityKey(@RequestBody SecurityKeyInfoFetchReq securityKeyInfoFetchReq) throws Exception {
        JSONObject jsonObject = new JSONObject();
        String securityKey = securityKeyInfoService.selectSecurityKey(securityKeyInfoFetchReq);
        if (StringUtils.isNotEmpty(securityKey)) {//成功
            jsonObject.put("code", 200);
            jsonObject.put("msg", "请求成功");
            HashMap<String, String> map = new HashMap<>();
            map.put("key", securityKey);
            jsonObject.put("data", map);
        }else {
            jsonObject.put("code", 500);
            jsonObject.put("msg", "请求失败");
            jsonObject.put("data", new HashMap());
        }
        return jsonObject;
    }

    /**
     * 获取服务端公钥和AES密钥
     * @param body
     * @return
     */
    @PostMapping("/test/request")
    public Object testRequest(@RequestBody Map<String, String> body) throws Exception {
        JSONObject jsonObject = new JSONObject();
        String encryptString = securityKeyInfoService.testRequest(body.get("encryptString"), body.get("channelId"));
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("encryptData",encryptString);
        jsonObject.put("data",jsonObject1);
        jsonObject.put("code", 200);
        jsonObject.put("msg", "请求成功");
        return jsonObject;
    }
}
